package com.tarena.lbs.basic.web.assemble;

import com.alibaba.fastjson2.JSON;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.pojo.basic.bo.BusinessBO;
import com.tarena.lbs.pojo.basic.vo.BusinessVO;
import com.tarena.lbs.pojo.basic.vo.DictItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class BusinessAssembler {
    //利用一个query对象+ basePageList 封装pageResult
    public PageResult<BusinessVO> assemblePageResultVO(BasePageList<BusinessBO> baseBos, BasePageQuery query){
        PageResult<BusinessVO> pageVOS = new PageResult<>();
        pageVOS.setPageNo(query.getPageNo());
        pageVOS.setPageSize(query.getPageSize());
        pageVOS.setTotal(baseBos.getTotal());
        List<BusinessVO> vos = new ArrayList<>();
        if (pageVOS.getTotal()>0){
            vos = bos2vos(baseBos.getList());
        }
        pageVOS.setObjects(vos);
        return pageVOS;
    }

    public  List<BusinessVO> bos2vos(List<BusinessBO> bos){
        List<BusinessVO> voList = new ArrayList<>();
        //加一个if判断 查询的分页数据
        if (CollectionUtils.isNotEmpty(bos)){
            voList=bos.stream().map(bo->{
                BusinessVO vo = new BusinessVO();
                BeanUtils.copyProperties(bo,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return voList;
    }

    public List<DictItemVO> assembleStaticBusinessTypes(){
        List<DictItemVO> dictItemVOS=new ArrayList<>();
        String values="[\n" +
                "    {\n" +
                "        \"id\":89,\n" +
                "        \"dictId\": 34,\n" +
                "        \"label\":\"美食餐饮\",\n" +
                "        \"dictValue\":\"美食餐饮\",\n" +
                "        \"sort\":10,\n" +
                "        \"status\":0,\n" +
                "        \"remark\":\"美食餐饮\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"id\":90,\n" +
                "        \"dictId\": 34,\n" +
                "        \"label\":\"旅行探险\",\n" +
                "        \"dictValue\":\"旅行探险\",\n" +
                "        \"sort\":10,\n" +
                "        \"status\":0,\n" +
                "        \"remark\":\"旅行探险\"\n" +
                "    }\n" +
                "]";
        dictItemVOS = JSON.parseArray(values, DictItemVO.class);
        return dictItemVOS;
    }
}
